import {
  modifyZubie,
  addZubie,
  closeGameProjectZubie,
  openProjectZubie,
  queryFenzuListForZubie,
  queryZubieByCode,
  changeZubieStatus,
  beginChouqian,
  queryPosition,
  autoChouqian,
  queryChouqian,
  queryAllZubieForGame,
  queryZhixu,
  queryScoreData,
  cancelChouqian,
  queryPhase,
  setKongchang,
  cancelSetPlace,
  modifyTeamInfo,
  cancelPhaseChouqian,
  deletePhase,
  queryScore,
  queryFanganHeader,
  queryFangan,
  queryZhixuForChouqian,
  queryNotSetPlaceGame,
  finishMatch,
  jiaohuanChangxu,
} from '../services/zubie';

export default {
  namespace: 'zubie',

  state: {
    zubieList: [],
    fenzuList: [],
    curZubieObj: {},
    positionList: [],
    chouqianList: {},
    zhixuList: {},
    zhixuScoreList: { list: [] },
    zhixuChouqianList: {},
    phaseList: [],
    scoreList: {},
    fanganHeader: {},
    fanganList: [],
    notSetPlaceGameList: [],
  },

  effects: {
    *addZubie({ payload, callback }, { select, call, put }) {
      const response = yield call(addZubie, payload);
      if (response) {
        if (callback) callback();
        yield put({
          type: 'gameProject/findGameProject',
        });
      }
    },
    *modifyZubie({ payload, callback }, { select, call, put }) {
      const response = yield call(modifyZubie, payload);
      if (response) {
        if (callback) callback();
        yield put({
          type: 'gameProject/findGameProject',
        });
      }
    },
    *closeGameProjectZubie({ payload }, { select, call, put }) {
      const response = yield call(closeGameProjectZubie, payload);
      if (response) {
        const curGame = yield select(state => state.gameList.modifyItem);
        yield put({
          type: 'gameProject/findGameProject',
        });
      }
    },
    *openProjectZubie({ payload }, { select, call, put }) {
      const response = yield call(openProjectZubie, payload);
      if (response) {
        const curGame = yield select(state => state.gameList.modifyItem);
        yield put({
          type: 'gameProject/findGameProject',
        });
      }
    },
    *queryAllZubieForGame({ payload, callback }, { select, call, put }) {
      const curGame = yield select(state => state.gameList.modifyItem);
      const response = yield call(queryAllZubieForGame, curGame.code);
      if (response) {
        yield put({
          type: 'changeState',
          payload: { zubieList: Array.isArray(response) ? response : [] },
        });
        if (callback) callback();
      }
    },
    *queryZubieByCode({ payload }, { select, call, put }) {
      const response = yield call(queryZubieByCode, payload);
      yield put({
        type: 'changeState',
        payload: {
          curZubieObj: response,
        },
      });
    },
    *queryFenzuListForZubie({ payload }, { select, call, put }) {
      const { zubieCode } = payload;

      yield put({
        type: 'queryFenzuByCode',
        payload: zubieCode,
      });

      const response = yield call(queryFenzuListForZubie, payload);
      yield put({
        type: 'changeState',
        payload: {
          fenzuList: Array.isArray(response) ? response : [],
        },
      });
    },
    *changeZubieStatus({ payload }, { select, call, put }) {
      const response = yield call(changeZubieStatus, payload);
      yield put({
        type: 'queryFenzuByCode',
        payload: payload.zubieCode,
      });
    },
    *modifyTeamInfo({ payload, callback }, { select, call, put }) {
      const response = yield call(modifyTeamInfo, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *beginChouqian({ payload, callback }, { select, call, put }) {
      const response = yield call(beginChouqian, payload);
      if (response) {
        yield put({
          type: 'queryFenzuListForZubie',
          payload,
        });
        yield put({
          type: 'queryChouqian',
          payload,
        });
        yield put({
          type: 'findPhaseTeamByZubie',
          payload,
        });
        const curZubieObj = yield select(state => state.zubie.curZubieObj);
        curZubieObj.status = 'BC';
        yield put({
          type: 'changeState',
          payload: {
            curZubieObj: curZubieObj,
          },
        });
        if (callback) callback();
      }

      // yield put({
      //   type: 'queryPosition',
      //   payload:payload.zubieCode,
      // });
    },
    *cancelChouqian({ payload, callback }, { select, call, put }) {
      const response = yield call(cancelChouqian, payload);
      if (response) {
        yield put({
          type: 'queryFenzuByCode',
          payload: payload.zubieCode,
        });
        const curZubieObj = yield select(state => state.zubie.curZubieObj);
        curZubieObj.status = 'NEW';
        yield put({
          type: 'changeState',
          payload: {
            curZubieObj: curZubieObj,
          },
        });
        yield put({
          type: 'changeState',
          payload: {
            chouqianList: {},
            zhixuList: {},
            fenzuList: [],
            zhixuTreeList: [],
            zhixuNotTreeList: [],
            zhixuChouqianList: {},
            mingdanList: [],
          },
        });
        if (callback) callback();
      }

      // yield put({
      //   type: 'queryPosition',
      //   payload:payload.zubieCode,
      // });
    },

    *cancelPhaseChouqian({ payload, callback }, { select, call, put }) {
      const response = yield call(cancelPhaseChouqian, payload);
      if (response) {
        yield put({
          type: 'queryChouqian',
          payload: payload,
        });
        if (callback) callback();
      }
    },

    *queryPosition({ payload }, { select, call, put }) {
      const response = yield call(queryPosition, payload);
      yield put({
        type: 'changeState',
        payload: {
          positionList: Array.isArray(response) ? response : [],
        },
      });
    },
    *autoChouqian({ payload, callback }, { select, call, put }) {
      const response = yield call(autoChouqian, payload);
      if (response) {
        yield put({
          type: 'queryChouqian',
          payload: payload,
        });
        yield put({
          type: 'findPhaseTeamByZubie',
          payload,
        });
        if (callback) callback();
        const curZubieObj = yield select(state => state.zubie.curZubieObj);
        curZubieObj.status = 'AC';
      }
    },
    *queryChouqian({ payload }, { select, call, put }) {
      const response = yield call(queryChouqian, payload);
      yield put({
        type: 'changeState',
        payload: {
          chouqianList: response,
        },
      });
    },

    *queryZhixu({ payload }, { select, call, put }) {
      const response = yield call(queryZhixu, payload);
      yield put({
        type: 'changeState',
        payload: {
          zhixuList: response,
        },
      });
    },
    *queryNotSetPlaceGame({ payload }, { select, call, put }) {
      const response = yield call(queryNotSetPlaceGame, payload);
      yield put({
        type: 'changeState',
        payload: {
          notSetPlaceGameList: response,
        },
      });
    },
    *queryScore({ payload }, { select, call, put }) {
      const response = yield call(queryScore, payload);
      yield put({
        type: 'changeState',
        payload: {
          scoreList: response,
        },
      });
    },
    *queryFanganHeader({ payload, callback }, { select, call, put }) {
      const response = yield call(queryFanganHeader, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            fanganHeader: response,
          },
        });
        if (callback) callback();
      }
    },
    *queryFangan({ payload }, { select, call, put }) {
      const response = yield call(queryFangan, payload);
      yield put({
        type: 'changeState',
        payload: {
          fanganList: Array.isArray(response) ? response : [],
        },
      });
    },
    *queryZhixuForChouqian({ payload }, { select, call, put }) {
      if (!payload.curPage) {
        payload.curPage = 1;
      }
      const response = yield call(queryZhixuForChouqian, payload);
      yield put({
        type: 'changeState',
        payload: {
          zhixuChouqianList: response,
        },
      });
    },
    *queryScoreData({ payload }, { select, call, put }) {
      if (!payload.curPage) {
        payload.curPage = 1;
      }
      const response = yield call(queryScoreData, payload);
      yield put({
        type: 'changeState',
        payload: {
          zhixuScoreList: response,
        },
      });
    },
    *queryPhase({ payload, callback }, { select, call, put }) {
      const response = yield call(queryPhase, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            phaseList: Array.isArray(response) ? response : [],
          },
        });
        if (callback) callback();
      }
    },
    *setKongchang({ payload, callback }, { select, call, put }) {
      const response = yield call(setKongchang, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *jiaohuanChangxu({ payload, callback }, { select, call, put }) {
      const response = yield call(jiaohuanChangxu, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *cancelSetPlace({ payload, callback }, { select, call, put }) {
      const response = yield call(cancelSetPlace, payload.code);
      if (response) {
        if (callback) callback();
      }
    },
    *finishMatch({ payload, callback }, { select, call, put }) {
      const response = yield call(finishMatch, payload.code);
      if (response) {
        if (callback) callback();
      }
    },
    *deletePhase({ payload, callback }, { select, call, put }) {
      const response = yield call(deletePhase, payload);
      if (response) {
        yield put({
          type: 'gameProject/findGameProject',
          payload: {},
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clean(state, payload) {
      return {
        ...state,
        fenzuList: [],
        curZubieObj: {},
        positionList: [],
        chouqianList: {},
        zhixuList: {},
        phaseList: [],
        scoreList: {},
        fanganHeader: {},
        fanganList: [],
        notSetPlaceGameList: [],
        zhixuChouqianList: {},
        zhixuScoreList: {},
      };
    },
  },
};
